package com.tanhua.app.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.tanhua.api.db.UserInfoApi;
import com.tanhua.api.mongo.CommentApi;
import com.tanhua.api.mongo.FriendsApi;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mogo.Comment;
import com.tanhua.domain.mogo.Friend;
import com.tanhua.domain.vo.ContactVo;
import com.tanhua.domain.vo.PageBeanVo;
import com.tanhua.domain.vo.UserCommentVo;
import com.tanhua.domain.vo.UserHolder;
import com.tanhua.template.HuanXinTemplate;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class MessagesService {


    @Reference
    private FriendsApi friendsApi;

    @Reference
    private UserInfoApi userinfoApi;

    @Reference
    private CommentApi commentApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    public ResponseEntity contacts(long userId, Long id) {
        // id：自己的 userId：对方的
        friendsApi.contacts(id,userId); //探花业务加好友
        huanXinTemplate.addContacts(id,userId); // 环信业务加好友
        return ResponseEntity.ok(null);
    }

    //查找用户好友列表
    public ResponseEntity findFriendsList(Integer page, Integer pagesize,Long id) {
        // 1 带着当前登录用户的id查询好友（分页查询）
        PageBeanVo pageBeanVo=friendsApi.findeachLovePage(page,pagesize,id);
        List<Friend> items = (List<Friend>) pageBeanVo.getItems();

        List<ContactVo> voList=new ArrayList();
        if(CollectionUtil.isNotEmpty(items)){
            // 2 根据查到的好友id 查询好友的详情信息 userInfo表
            for (Friend friend : items) {
                //查找好友,获取好友详细信息
                UserInfo userInfo = userinfoApi.findUserInfo(friend.getFriendId());
                ContactVo vo = new ContactVo();
                //封装好友数据到vo
                BeanUtil.copyProperties(userInfo,vo);
                vo.setUserId(userInfo.getId().toString());
                vo.setCity(userInfo.getCity().split("-")[0]);
                voList.add(vo);
            }
        }
        // 3 组装pageBean返回
        pageBeanVo.setItems(voList);
        return ResponseEntity.ok(pageBeanVo);
    }

    //分页查询1点赞  2评论 3喜欢 的用户列表
    public ResponseEntity findPageUserInfo(Integer page, Integer pagesize, int commentId) {
        // 1 分页查询quanzi_comment的数据
        PageBeanVo pageBeanVo=commentApi.findPageComment(page,pagesize,commentId,UserHolder.getUser().getId());
        List<Comment> items = (List<Comment>) pageBeanVo.getItems();

        List<UserCommentVo> voList =new ArrayList();
        if(CollectionUtil.isNotEmpty(items)){
            for (Comment comment : items) {
                //2 查询userInfo的数据-获取点赞、评论、喜欢用户
                UserInfo userInfo = userinfoApi.findUserInfo(comment.getUserId());
                UserCommentVo vo = new UserCommentVo();
                BeanUtil.copyProperties(userInfo,vo);
                vo.setCreateDate(DateUtil.format(new Date(comment.getCreated()),"yyyy-MM-dd HH:mm"));
                voList.add(vo);
            }
        }
        //3 封装pageBean返回
            pageBeanVo.setItems(voList);
        return ResponseEntity.ok(pageBeanVo);
    }
}
